<template>
  <div>
    <cement-content 
    :title="boreholeTitle" 
    :search="boreholeSearch" 
    @getData="getBurstPressure" 
    @deleteRow="deleteBurstPressure"
    @submitUpdate="submitBurstPressureUpdate" 
    @submitSearchModel="submitBoreholeSearchModel">
    </cement-content>
  </div>
</template>

<script>
  import cementContent from '@/components/cement-content'
  import { BURST_PRESSURE_TITLE, BOREHOLE_SEARCH_DATA, FIRST_PAGE, SEPARATOR } from '@/util/constant'
  import { mapGetters, mapMutations } from 'vuex'
  import { getBurstPressure, deleteBurstPressure, updateBurstPressure } from '@/api/module/burst-pressure'
  import { get, cloneDeep } from 'lodash'
  export default {
    name: 'index',
    components: {
      cementContent
    },
    data() {
      return {
        boreholeTitle: BURST_PRESSURE_TITLE,
        boreholeSearch: BOREHOLE_SEARCH_DATA
      }
    },
    methods: {
      ...mapMutations([
        'changeTableData',
        'changeTotal',
        'closeDialog',
        'changePageSize'
      ]),
      getBurstPressure() {
        let search = cloneDeep(this.searchModelGetters)
        if (get(this.searchModelGetters, 'drillDate')) {
          search['drillDate'] = this.searchModelGetters['drillDate'][0] + SEPARATOR + this.searchModelGetters['drillDate'][1]
        }
       getBurstPressure(this.currentPageGetters - 1, this.pageSizeGetters, search).then((res) => {
          this.changeTableData(get(res, 'data.data.content', []))
          this.changeTotal(get(res, 'data.data.totalElements', 0))
        })
      },
      deleteBurstPressure(row) {
        if (get(row, 'BurstPressureId')) {
          deleteBurstPressure(row['BurstPressureId'])
        }
      },
      submitBoreholeSearchModel() {
        this.changePageSize(FIRST_PAGE)
        this.getBurstPressure()
      },
      submitBurstPressureUpdate() {
        if (get(this.updateModelGetters, 'BurstPressureId')) {
          updateBurstPressure(get(this.updateModelGetters, 'BurstPressureId'), this.updateModelGetters).then(
            this.closeDialog()
          )
        }
      }
    },
    computed: {
      ...mapGetters([
        'pageSizeGetters',
        'currentPageGetters',
        'searchModelGetters',
        'updateModelGetters'
      ])
    },
    mounted() {
      this.getBurstPressure()
    }
  }
</script>